package starfire.ftp;

import java.io.*;
import java.util.*;

/**
 * @author tianshi
 *
 */
public class FtpFile
    extends File
    implements Serializable {

  String path = null;
  String name = null;
  String createTime = "";
  long length = 0L;
  boolean isDir = false;
  String attribute = "";
  FtpFile parent = null;

  protected Vector children = null;

  public FtpFile(String name) {
    super(name);
    this.name = name;
  }

  public static FtpFile getDirectory(String name)
  {
    FtpFile f = getDefaultRoot();
    f.setPath(name);
    return f;
  }

  public static FtpFile getDefaultRoot() {
    FtpFile file = new FtpFile("");
    file.name = "/";
    file.isDir = true;
    file.children = new Vector(5);
    file.attribute = "read";
    file.createTime = "";
    return file;
  }

  public void setName(String name)
  {
    int index = name.indexOf("/");
    if (index!=-1) {
      setPath(name);
    }
    else {
      this.name = name;
    }
  }

  public void setAttribute(String attribute)
  {
    this.attribute = attribute;
  }

  public void setLength(long length)
  {
    this.length = length;
  }

  public void setCreateTime(String time)
  {
    this.createTime = time;
  }

  public void setIsDir(boolean isDir)
  {
    this.isDir = isDir;
    children = new Vector();
  }

  public String getParent() {
    if (parent == null) {
      return null;
    }
    return parent.getPath();
  }

  public File getParentFile() {
    return parent;
  }

  public boolean canRead() {
    return true;
  }

  public boolean canWrite() {
    return false;
  }

  public long length() {
    return length;
  }

  public String getCreateTime() {
    return createTime;
  }

  public boolean isDirectory() {
    return isDir;
  }

  public boolean isFile() {
    return!isDir;
  }

  public String getAttribute() {
    return attribute;
  }

  public File[] listFiles() {
    if (isDir) {
      Object[] objs = children.toArray();
      FtpFile[] files = new FtpFile[objs.length];
      for (int i = 0; i < objs.length; i++) {
        files[i] = (FtpFile) objs[i];
      }
      return files;
    }
    else {
      return null;
    }
  }

  public void setPath(String path)
  {
    this.path = path;
    int index = path.lastIndexOf('/');
    if (index!=-1)
      name = path.substring(index+1);
    else
      name = path;
  }

  public String getPath() {
    if (path!=null)
      return path;
    if (parent == null) {
      return "/";
    }
    else {
      if (parent.getPath().equals("/")) {
        return "/" + name;
      }
      else {
        return parent.getPath() + "/" + name;
      }
    }
  }

  public String getName() {
    return name;
  }

  public void setParent(FtpFile parent) {
    this.parent = parent;
  }

  public Iterator childrenIterator() {
    if (isDir) {
      return children.iterator();
    }
    else {
      return null;
    }
  }

  public Object[] childrenArray() {
    if (isDir) {
      return children.toArray();
    }
    else {
      return null;
    }
  }

  public void addChildren(FtpFile file) {
    if (isIncludeFile(file)) {
      return;
    }
    file.parent = this;
    children.add(file);
  }

  protected boolean isIncludeFile(FtpFile file) {
    Iterator iterator = children.iterator();
    FtpFile ff;
    while (iterator.hasNext()) {
      ff = (FtpFile) (iterator.next());
      if (ff.getName().equals(file.getName()) && file.isDir == ff.isDir) {
        return true;
      }
    }
    return false;
  }

  public void addChildrens(FtpFile[] files) {
    for (int i = 0; i < files.length; i++) {
      addChildren(files[i]);
    }
  }

  public int hashCode() {
    return super.hashCode() + toString().hashCode();
  }

  public String toString() {
    return getPath();
  }

}
